// pages/component/message/message.js
import routerManager from "../../../utils/routerManager.js";
import http from "../../../utils/http.js";
import util from "../../../utils/util.js";
import TIM from "tim-wx-sdk";

const app = getApp();

Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    conversationList: [],
    myAvatar: "",
    triggered:false,
  },
  ready() {
    let that = this;
    let tim = app.$tim;
    if (app.isTimReady) {
      tim
        .getConversationList()
        .then(function(imResponse) {
          const conversationList = imResponse.data.conversationList; // 会话列表，用该列表覆盖原有的会话列表
          for (let i = 0; i < conversationList.length; i++) {
            console.log("conversationList[i].lastMessage.lastTime",conversationList[i].lastMessage.lastTime)
            conversationList[i].lastMessage.lastTimeStr = util.formatTime(
              conversationList[i].lastMessage.lastTime * 1000,
              "yyyy-MM-dd hh:mm"
            );
          }
          console.log("conversationList", conversationList);
          that.setData({
            conversationList: conversationList
          });
        })
        .catch(function(imError) {
          console.warn("getConversationList error:", imError); // 获取会话列表失败的相关信息
        });
    } else {
      tim.on(TIM.EVENT.SDK_READY, function(event) {
        // 收到离线消息和会话列表同步完毕通知，接入侧可以调用 sendMessage 等需要鉴权的接口
        // event.name - TIM.EVENT.SDK_READY
        console.log("SDK READY");
        tim
          .getConversationList()
          .then(function(imResponse) {
            const conversationList = imResponse.data.conversationList; // 会话列表，用该列表覆盖原有的会话列表
            for (let i = 0; i < conversationList.length; i++) {
              console.log("conversationList[i].lastMessage.lastTime",conversationList[i].lastMessage.lastTime)
              conversationList[i].lastMessage.lastTimeStr = util.formatTime(
                conversationList[i].lastMessage.lastTime * 1000,
                "yyyy-MM-dd hh:mm"
              );
            }
            console.log("conversationList", conversationList);
            that.setData({
              conversationList: conversationList
            });
          })
          .catch(function(imError) {
            console.warn("getConversationList error:", imError); // 获取会话列表失败的相关信息
          });
      });
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    toChat(event) {
      let cid = event.currentTarget.dataset.cid;
      let uid = event.currentTarget.dataset.uid;
      let avatar = event.currentTarget.dataset.avatar;
      routerManager.toChat(cid, uid , avatar);
    },
    refreshMsgList() {
      console.log("refreshMsgList");
      let that = this;
      let tim = app.$tim;
      that.setData({
        triggered:true
      })
      tim
        .getConversationList()
        .then(function(imResponse) {
          const conversationList = imResponse.data.conversationList; // 会话列表，用该列表覆盖原有的会话列表
          for (let i = 0; i < conversationList.length; i++) {
            conversationList[i].lastMessage.lastTimeStr = util.formatTime(
              conversationList[i].lastMessage.lastTime * 1000,
              "yyyy-MM-dd hh:mm"
            );
          }
          console.log("conversationList", conversationList);
          that.setData({
            conversationList: conversationList,
            triggered:false
          });
        })
        .catch(function(imError) {
          console.warn("getConversationList error:", imError); // 获取会话列表失败的相关信息
        });
    }
  }
});
